home *** CD-ROM | disk | FTP | other *** search
- {==========================}
- { Unit for I/O Redirection }
- { Richard Casey }
- { CIS 72247,151 }
- {==========================}
-
- {===========================================================================
-
- This unit is used to redirect I/O to a specific file handle to a user
- specified file. Typically this would be used to redirect standard input
- or standard output (or both) before executing a program via the EXEC
- procedure.
-
- Thanks to Mike Rubenstein (70205,1144) for the information on how to do this.
-
- Thanks also to Neil Rubenking (72267,1531) for his EXECDEMO program.
-
- Types:
- None
-
- Procedures:
- RedirectInput Redirect a handle to a file (input)
- RedirectOutput Redirect a handle to a file (output)
- UnRedirect Restore a handle to its previous state
- ExecRedirect Exec a program with I/O redirection
- without loading another copy of COMMAND.COM
-
- Functions:
- None
-
- Revision History:
-
- 1.0: 12/22/88 - Initial version
- 2.0: 01/09/88 - Replaced Redirect with RedirectInput & RedirectOutput
- Added ExecRedirect
-
-
- ===========================================================================}
- Unit Redir;
-
- interface
-
- uses
- dos;
-
- procedure RedirectInput( Handle : word; {Handle to redirect }
- Filename : string; {File to redirect from }
- var SaveHandle : word); {Copy of original handle}
-
- procedure RedirectOutput( Handle : word; {Handle to redirect }
- Filename : string; {File to redirect to }
- var SaveHandle : word); {Copy of original handle}
-
- procedure UnRedirect(Handle, {Handle to redirect}
- SaveHandle : word); {Copy of original handle}
-
- procedure ExecRedirect( Command : PathStr; {Command to execute }
- InputFile : PathStr; {Redirected input file }
- OutputFile : PathStr; {Redirected output file}
- var Status : word); {DOS return code }
- { Use '' for InputFile and/or OutputFile to skip I/O redirection }
-
- {==========================================================================}
-
- implementation
-
- procedure Redirect( Handle : word; {Handle to redirect }
- Filename : string; {File to redirect from/to}
- IOtype : byte; {1 = input, 2 = output }
- var SaveHandle : word); {Copy of original handle }
- var
- f : file;
- r : registers;
- x : word;
- begin
- with r do begin
-
- SaveHandle:=$FFFF;
-
- assign(f,FileName); { Open desired file }
- {$I-}
- case IOtype of
- 1 : reset(f);
- 2 : rewrite(f);
- else exit;
- end;
- {$I+}
- if IOResult<>0 then exit;
-
- AX:=$4500; { Save current value of Handle }
- BX:=Handle;
- MsDos(r);
- if (Flags and fCarry)<>0 then exit;
- x:=r.ax;
-
- AX:=$4600; { Assign file handle to Handle }
- BX:=FileRec(f).Handle;
- CX:=Handle;
- MsDos(r);
- if (Flags and fCarry)<>0 then exit;
-
- close(f); { Close file }
- SaveHandle:=x; { Return saved value of Handle }
- end;
- end;
-
- procedure RedirectInput( Handle : word; {Handle to redirect }
- Filename : string; {File to redirect from }
- var SaveHandle : word); {Copy of original handle}
- begin
- Redirect(Handle,Filename,1,SaveHandle);
- end;
-
- procedure RedirectOutput( Handle : word; {Handle to redirect }
- Filename : string; {File to redirect to }
- var SaveHandle : word); {Copy of original handle}
- begin
- Redirect(Handle,Filename,2,SaveHandle);
- end;
-
- procedure UnRedirect(Handle, {Handle to redirect}
- SaveHandle : word); {Copy of original handle}
- var
- r : registers;
- begin
- with r do begin
- AX:=$4600; { Assign saved value back to Handle }
- BX:=SaveHandle;
- CX:=Handle;
- MsDos(r);
- if (Flags and fCarry)<>0 then exit;
-
- AX:=$3E00; { Close saved value }
- BX:=SaveHandle;
- MsDos(r);
- end;
- end;
-
- procedure ExecRedirect( Command : PathStr; {Command to execute }
- InputFile : PathStr; {Redirected input file }
- OutputFile : PathStr; {Redirected output file}
- var Status : word); {DOS return code }
- var
- Found : PathStr;
- SaveHandle0 : word;
- SaveHandle1 : word;
- begin
- { Find Command }
- { Try COM, EXE, and BAT extensions }
- { First on current directory, then on the PATH }
- Found := FSearch(Command+'.COM','');
- if Found = '' then Found := FSearch(Command+'.EXE','');
- if Found = '' then Found := FSearch(Command+'.BAT','');
- if Found = '' then Found := FSearch(Command+'.COM', GetEnv('PATH'));
- if Found = '' then Found := FSearch(Command+'.EXE', GetEnv('PATH'));
- if Found = '' then Found := FSearch(Command+'.BAT', GetEnv('PATH'));
- if Found = '' then begin
- Status:=$FFFF;
- exit;
- end;
- Found := FExpand(Found);
- SwapVectors;
- if Pos('.BAT',Found) > 0 then begin
- if InputFile<>'' then InputFile :=' <'+InputFile;
- if OutputFile<>'' then OutputFile:=' >'+OutputFile;
- Exec(GetEnv('COMSPEC'),'/C '+Found+InputFile+OutputFile);
- end else begin
- if InputFile<>'' then RedirectInput(0,InputFile,SaveHandle0);
- if OutputFile<>'' then RedirectOutput(1,OutputFile,SaveHandle1);
- Exec(Found,'');
- if InputFile<>'' then UnRedirect(0,SaveHandle0);
- if OutputFile<>'' then UnRedirect(1,SaveHandle1);
- end;
- SwapVectors;
- Status:=DOSError;
- end;
-
- end.
-